const Order = require("../model/order/order")
const UserAddresses = require("../model/user/user_addresses")
//获取订单列表
exports.getList = (req, res) => {
    let token=req.user
    Order.getList(token.id).then(json=>{
        res.js(json)
    })
}
//下订单
exports.add = (req, res) => {
    let token=req.user
    let id=token.id
    let cart_ids=req.body.cart_ids//购物车id组
    let address_id=req.body.address_id//地址id
    try{
        UserAddresses.getOne(address_id,id).then(value=>{
            UserAddresses.updateLastUserTime(address_id,id).then(()=>{
                Order.addOrder(id,cart_ids,value.toJSON()).then(value=>{
                    res.json(value.toJSON())
                })
            })
        })
    }catch(err){//库存不足捕获
        switch (err.name) {
            case "库存不足":
                res.cc({message:"库存不足",data:err.data})
                break;
        
            case "购物车不存在":
                res.cc({message:"购物车不存在",data:err.data})
                break;
            default:
                res.cc({message:"未知的错误"})
        } 
    }
}
//取消订单
exports.delete=(req,res)=>{
    let token=req.user
    let id=token.id
    let ids=req.body.ids
    Order.delete(id,ids).then(()=>{
        res.js(true)
    })
}